﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Center.core;
using Center.lib.utils.json;
using Center.lib.utils.http;
using Center.lib.utils.json.pager;
using Center.lib.utils.common;
using System.IO;

namespace Center.test
{
    class Program
    {
        static void Main(string[] args)
        {
            test1();
        }
        static void test1()

        {
            Dictionary<String, Dictionary<String, String>> map = new Dictionary<string, Dictionary<string, string>>();
            List<String> ids = new List<String>();
            List<String> sqls = new List<String>();
            StreamReader sr = new StreamReader(@"C:\FeigeDownload\0119.txt", Encoding.Default);  //path为文件路径
            String line;
            while ((line = sr.ReadLine()) != null)//按行读取 line为每行的数据
            {
                String[] sline = line.Split(new Char[] { '\t' });
                String id = sline[0];
                String userOrderCode = sline[2];
                String appId = sline[1];

                //Console.WriteLine(line);
                Console.WriteLine("正在加载。。。{0}",line);
                if (map.ContainsKey(appId))
                {
                    if (map[appId].ContainsKey(userOrderCode))
                    {
                        //Console.WriteLine("重复 appId:{0},userOrderCode:{1}",appId,userOrderCode);
                        sqls.Add(String.Format("delete from user_order where id={0};", map[appId][userOrderCode]));
                        ids.Add(id);
                    }
                    else
                    {
                        map[appId].Add(userOrderCode, id);
                    }
                }
                else
                {
                    
                    Dictionary<String, String> uMap = new Dictionary<string, string>();
                    uMap.Add(userOrderCode, id);
                    map.Add(appId, uMap);
                }
            }
            Console.WriteLine("重复数量：{0}", ids.Count);

            FileStream fs = new FileStream(@"D:\xiangmu\shop\0101-id.sql", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sqls.ForEach(p => { sw.WriteLine(p); });
            //开始写入

            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
            Console.Read();
        }
        static void test2()
        {
            Dictionary<String, Dictionary<String, String>> map = new Dictionary<string, Dictionary<string, string>>();
            List<String> ids = new List<String>();
            List<String> sqls = new List<String>();
            List<String> sqlss = new List<String>();
            List<String> userCodes = new List<String>();
            Dictionary<String, double> userIds = new Dictionary<string, double>();
            StreamReader sr = new StreamReader(@"D:\xiangmu\shop\1.txt", Encoding.UTF8);  //path为文件路径
            String line;
            while ((line = sr.ReadLine()) != null)//按行读取 line为每行的数据
            {
                String[] sline = line.Split(new Char[] { '\t' });
                String id = sline[0];
                String userOrderCode = sline[1];
                String appId = sline[2];

                //Console.WriteLine(line);

                if (map.ContainsKey(appId))
                {
                    if (map[appId].ContainsKey(userOrderCode))
                    {
                        String[] ss = userOrderCode.Split(new Char[] { '，' });
                        String st = ss[0].Replace("退款：", "");
                        Console.WriteLine("金额：{0}", st);
                        double money1 = Double.Parse(st);
                        if (userIds.ContainsKey(appId))
                        {
                            userIds[appId] = userIds[appId] + money1;
                        }
                        else
                        {
                            userIds.Add(appId, money1);
                        }
                        sqls.Add(String.Format("delete from user_order where id={0};", map[appId][userOrderCode]));
                        ids.Add(id);
                        userCodes.Add( "用户id"+appId+"\t"+userOrderCode);
                    }
                    else
                    {
                        map[appId].Add(userOrderCode, id);
                    }
                }
                else
                {
                    Dictionary<String, String> uMap = new Dictionary<string, string>();
                    uMap.Add(userOrderCode, id);
                    map.Add(appId, uMap);
                }
            }
            Console.WriteLine("重复数量：{0}", ids.Count);
            Double money = 0;
            foreach (KeyValuePair<string, double> kvp in userIds)
            {
                money += kvp.Value;
                //sqlss.Add(String.Format("update user_order set money=money-{0} where user_id={1};", kvp.Value, kvp.Key));
                sqlss.Add(String.Format("{0}\t{1}", kvp.Value, kvp.Key));
            }
            Console.WriteLine("总金额：{0}", money);

            writeFile(sqls, @"D:\xiangmu\shop\1111.sql");
            writeFile(sqlss, @"D:\xiangmu\shop\1111-u.sql");
            writeFile(userCodes, @"D:\xiangmu\shop\all.txt");

            Console.Read();
        }

        static void writeFile(List<String> list, String filepath)
        {

            FileStream fss = new FileStream(filepath, FileMode.Create);
            StreamWriter sww = new StreamWriter(fss);
            list.ForEach(p => { sww.WriteLine(p); });
            //开始写入
            //清空缓冲区
            sww.Flush();
            //关闭流
            sww.Close();
            fss.Close();
        }
    }
}
